package com.edouardbataille.open.handler
{
	import __AS3__.vec.Vector;
	
	import com.edouardbataille.open.MySqlSocket;
	import com.edouardbataille.open.bytes.ByteReader;
	import com.edouardbataille.open.mysql;
	import com.edouardbataille.open.requester.PreparedStatement;
	
	import flash.events.EventDispatcher;
	
	import mx.rpc.Fault;
	import mx.rpc.events.FaultEvent;
	
	[Event (name="fault", type="mx.rpc.events.FaultEvent")]
	public class StatementOkHandler extends EventDispatcher implements IHandler
	{
		private var con : MySqlSocket;
		
		private var stmt : PreparedStatement;
	
		public function StatementOkHandler (stmt : PreparedStatement)
		{
			this.stmt = stmt;
		}
		
		public function handleData (packets:Vector.<ByteReader>, con : MySqlSocket) : void
		{
			var data : ByteReader = packets.shift ();
			data.position = 4;
			var ok : uint = data.readByte() & 0xFF;
			if (ok == 0xFF)
			{
				var fault : Fault = FaultParser.parseFault(data);
				dispatchEvent(FaultEvent.createEvent(fault));
				return;
			}	
			var statementId : uint = data.readInt();
			var nbCol : uint = data.readMultipleByte(2);
			var nbParam : uint = data.readMultipleByte(2);
			if (data.readByte() == 0)
			{
				var warning : uint = data.readMultipleByte(2);
				stmt.mysql::statementId = statementId;
			}
		}
	}
}